<?php
/* post type for main slider */
$labels = array(
    'name'                  => _x('Slider', 'post type general name', LANGUAGE_ZONE),
    'singular_name'         => _x('Slide', 'post type singular name', LANGUAGE_ZONE),
    'add_new'               => _x('Add New Slide', 'post type new', LANGUAGE_ZONE),
    'add_new_item'          => __('Add New Slide', LANGUAGE_ZONE),
    'edit_item'             => __('Edit Slide', LANGUAGE_ZONE),
    'new_item'              => __('New Slide', LANGUAGE_ZONE),
    'view_item'             => __('View Slide', LANGUAGE_ZONE),
    'search_items'          => __('Search for Slides', LANGUAGE_ZONE),
    'not_found'             =>  __('No Slides Found', LANGUAGE_ZONE),
    'not_found_in_trash'    => __('No Slides found in Trash', LANGUAGE_ZONE), 
    'parent_item_colon'     => '',
    'menu_name'             => 'Slider'
);

$img = get_template_directory_uri(). '/images/admin_ico_slides.png';

$args = array(
    'labels'                => $labels,
    'public'                => false,
    'publicly_queryable'    => false,
    'show_ui'               => true, 
    'show_in_menu'          => true, 
    'query_var'             => true,
    'rewrite'               => false,
    'capability_type'       => 'post',
    'has_archive'           => false, 
    'hierarchical'          => false,
    'menu_position'         => 26,
    'menu_icon'             => $img,
    'supports'              => array( 'thumbnail', 'title', 'excerpt' )
);
register_post_type( 'dt_slider', $args);
		
// taxonomy
$labels = array(
    'name'                  => _x( 'Categories', 'taxonomy general name', LANGUAGE_ZONE ),
    'singular_name'         => _x( 'Category', 'taxonomy singular name', LANGUAGE_ZONE ),
    'search_items'          => __( 'Search in Category', LANGUAGE_ZONE ),
    'all_items'             => __( 'Categories', LANGUAGE_ZONE ),
    'parent_item'           => __( 'Parent Category', LANGUAGE_ZONE ),
    'parent_item_colon'     => __( 'Parent Category:', LANGUAGE_ZONE ),
    'edit_item'             => __( 'Edit Category', LANGUAGE_ZONE ), 
    'update_item'           => __( 'Update Category', LANGUAGE_ZONE ),
    'add_new_item'          => __( 'Add New Category', LANGUAGE_ZONE ),
    'new_item_name'         => __( 'New Category Name', LANGUAGE_ZONE ),
    'menu_name'             => __( 'Categories', LANGUAGE_ZONE ),
); 	
/*
register_taxonomy('dt_slider_category',array('dt_slider'), array(
    'hierarchical'          => true,
    'show_in_nav_menus '    => false,
    'public'                => false,
    'show_tagcloud'         => false,
    'labels'                => $labels,
    'show_ui'               => true,
    'rewrite'               => false
));
/* Define the custom box */

// WP 3.0+
add_action( 'add_meta_boxes', 'slider_meta_box' );

add_action( 'save_post', 'dt_slider_inner_box_save' );
add_action( 'save_post', 'dt_home_slider_options_save' );

/* Adds a box to the main column on the Post and Page edit screens */
function slider_meta_box () {
    add_meta_box ( 
        'dt_slider-post',
        __( 'Slide options', LANGUAGE_ZONE ),
        'dt_slider_inner_box',
        'dt_slider',
        'side'
    );

    if ( isset($_GET['post']) ) {
        $post_id = $_GET['post'];
    } elseif ( isset($_POST['post_ID']) ) {
        $post_id = $_POST['post_ID'];
    } else
        return;
    
    $page_meta = get_post_meta($post_id,'_wp_page_template',TRUE);
    if( 'dt_home.php' == $page_meta ||
        'dt_blog_slider.php' == $page_meta ||
        'dt_home_full.php' == $page_meta ) {
        add_meta_box( 
            'dt_slider-layout',
            __( 'Slider options', LANGUAGE_ZONE ),
            'dt_home_slider_options',
            'page'
        );
    }
}

/* Prints the box content */
function dt_slider_inner_box( $post ) {
    $box_name = 'dt_slider-post';
    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), $box_name. '_nonce' );

    $defaults = array(
        'link'      => '',
        'hide_desc' => false
    );
    $opts = get_post_meta( $post->ID, $box_name, true );
    $opts = wp_parse_args( $opts, $defaults );
    
    ?>
    
    <p>
        <label>
            <?php _e( 'Slide link', LANGUAGE_ZONE ); ?>
            <input type="text" id="<?php echo $box_name; ?>_link" name="<?php echo $box_name; ?>_link" value="<?php echo esc_attr($opts['link']); ?>" size="43" />
        </label>
    </p>
    <p>
        <label>
            <input type="checkbox" id="<?php echo $box_name; ?>_hide_desc" name="<?php echo $box_name; ?>_hide_desc" <?php checked($opts['hide_desc']); ?>/>
            <?php _e( 'Hide post text in the side box of slide', LANGUAGE_ZONE ); ?>
        </label>
    </p>
   <?php
}

/* When the post is saved, saves our custom data */
function dt_slider_inner_box_save( $post_id ) {
    $box_name = 'dt_slider-post';
    
    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
        return;

    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times

    if ( !isset( $_POST[$box_name. '_nonce'] ) || !wp_verify_nonce( $_POST[$box_name. '_nonce'], plugin_basename( __FILE__ ) ) )
        return;

    // Check permissions
    if ( !current_user_can( 'edit_post', $post_id ) )
        return;

    // OK, we're authenticated: we need to find and save the data
    $mydata = array();
    
    if( isset($_POST[$box_name. '_link']) ) {
        $mydata['link'] = esc_url_raw( $_POST[$box_name. '_link'] );
    }
    
    $mydata['hide_desc'] = isset( $_POST[$box_name. '_hide_desc'] );

    update_post_meta( $post_id, $box_name, $mydata );
}

// SLIDER METABOX
function dt_home_slider_options( $post ) {
    $box_name = 'dt_slider-layout';
   
    $defaults = array(
        'timeout'   => 10
    );
    $opts = get_post_meta( $post->ID, $box_name, true );
    $opts = wp_parse_args( $opts, $defaults );

    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), $box_name. '_nonce' );

    // The actual fields for data entry
    ?>
    
    <p>
        <label>
            <input type="text" id="<?php echo $box_name; ?>_timeout" name="<?php echo $box_name; ?>_timeout" value="<?php echo intval($opts['timeout']); ?>" size="4"/>
            <?php _e( 'Slider timeout(in seconds)', LANGUAGE_ZONE ); ?>
        </label>
    </p>
    
    <?php
    // print select list
/*    dt_admin_select_list(
        array(
            'rad_butt_name' => 'show_type_slider',
            'checkbox_name' => 'show_slider',
            'terms'         => &$terms,
            'current_sel'   => $opts['list'],
            'con_class'     => 'dt_slider-layout'
        )    
    );
*/    ?>
    
    <?php
}

// SLIDER SAVE FUNK
function dt_home_slider_options_save( $post_id ) {
    $box_name = 'dt_slider-layout';
    
    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
        return;

    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times

    if ( !isset( $_POST[$box_name. '_nonce'] ) || !wp_verify_nonce( $_POST[$box_name. '_nonce'], plugin_basename( __FILE__ ) ) )
        return;

    if ( !current_user_can( 'edit_post', $post_id ) )
        return;
    
    // OK, we're authenticated: we need to find and save the data
    $mydata = array();
    
    if( isset($_POST[$box_name. '_timeout']) ) {
        $mydata['timeout'] = intval($_POST[$box_name. '_timeout']);
    } 
/*    
    switch( $_POST['show_type_slider'] ) {
        case 'only':
            if( isset($_POST['show_slider']['only']) ) {
                $mydata['list']['only'] = $_POST['show_slider']['only'];
            }
            break;
        case 'except':
            if( isset($_POST['show_slider']['except']) ) {
                $mydata['list']['except'] = $_POST['show_slider']['except'];
            }
            break;
        default:
            $mydata['list'] = null;
    }
*/    
    update_post_meta( $post_id, $box_name, $mydata );
}			